package cn.com.header.core.support.web.springmvc;

import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.html.HtmlEscapers;
import org.springframework.core.convert.converter.Converter;

import java.util.Objects;

/**
 * @author liangzhongqiu789@sina.com
 * @date 2017-06-26
 * @time 10:43
 */
public abstract class AbstractConverter<T> implements Converter<String,T> {


    private boolean escapeHtml = true;//是否处理html特殊标识
    private boolean trimContent = true;//是否处理两端的空白
    private boolean convertWhitespaceToNull = true;//是否过滤忽略空白

    @Override
    public T convert(String source) {
        String thisParse = source;
        if(Objects.nonNull(thisParse)){
            thisParse = trimContent ? CharMatcher.whitespace().trimFrom(thisParse) : thisParse;
            if (!Strings.isNullOrEmpty(thisParse)) {
                if (escapeHtml) {
                    thisParse = HtmlEscapers.htmlEscaper().escape(thisParse);
                }
            } else if (convertWhitespaceToNull) {
                return null;
            }
        }
        return convertAfterCheck(thisParse);
    }

    public abstract T convertAfterCheck(String source);

    public void setEscapeHtml(boolean escapeHtml) {
        this.escapeHtml = escapeHtml;
    }

    public void setTrimContent(boolean trimContent) {
        this.trimContent = trimContent;
    }

    public void setConvertWhitespaceToNull(boolean convertWhitespaceToNull) {
        this.convertWhitespaceToNull = convertWhitespaceToNull;
    }

    public boolean isEscapeHtml() {
        return escapeHtml;
    }

    public boolean isTrimContent() {
        return trimContent;
    }

    public boolean isConvertWhitespaceToNull() {
        return convertWhitespaceToNull;
    }
}
